﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class SmartTriggerDomainService : ISmartTriggerDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, SmartTrigger> _repository;
        public IMapper Mapper { get; set; }
        public SmartTriggerDomainService(IRepository<Guid, SmartTrigger> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public SmartTrigger Create(SmartTrigger smarttrigger)
        {
            smarttrigger.Id = Guid.NewGuid();
            if (smarttrigger.Conditions != null && smarttrigger.Conditions.Count > 0)
            {
                ISmartTriggerConditionDomainService conditionDomainService = _container.Resolve<ISmartTriggerConditionDomainService>();
                foreach (var con in smarttrigger.Conditions)
                {
                    con.Id = Guid.NewGuid();
                    con.SmartTriggerId = smarttrigger.Id;
                    conditionDomainService.Create(con);
                }                
            }
            if (smarttrigger.Actions != null && smarttrigger.Actions.Count > 0)
            {
                ISmartTriggerActionDomainService actionDomainService = _container.Resolve<ISmartTriggerActionDomainService>();
                foreach (var action in smarttrigger.Actions)
                {
                    action.Id = Guid.NewGuid();
                    action.SmartTriggerId = smarttrigger.Id;
                    actionDomainService.Create(action);
                }
            }
            _repository.Create(smarttrigger);
            return GetbyId(smarttrigger.Id);
        }

        public SmartTrigger Get(Guid id)
        {
            return _repository.Get(id);
        }

        public SmartTrigger Update(SmartTriggerUpdateBo bo)
        {
            SmartTrigger smartTrigger = _repository.Get(bo.Id);
            //smartTrigger.Id = bo.Id;
            //smartTrigger.BotId = bo.BotId;
            smartTrigger.Name = bo.Name;
            smartTrigger.IsEnabled = bo.IsEnabled;
            smartTrigger.ConditionExpressionType = bo.ConditionExpressionType;
            smartTrigger.LogicalExpression = bo.LogicalExpression;
            smartTrigger.Order = bo.Order;

            if (bo.Conditions != null && bo.Conditions.Count > 0)
            {
                ISmartTriggerConditionDomainService conditionDomainService = _container.Resolve<ISmartTriggerConditionDomainService>();
                List<Guid> conditionIds = new List<Guid> { };

                foreach (var item in bo.Conditions)
                {
                    if (item.Id != Guid.Empty)
                    {
                        //修改      
                        item.SmartTriggerId = bo.Id;
                        conditionDomainService.Update(item);
                        conditionIds.Add(item.Id);
                    }
                    else
                    {
                        //新增
                        SmartTriggerCondition condition = Mapper.Map<SmartTriggerCondition>(item);
                        condition.Id = Guid.NewGuid();
                        item.Id = condition.Id;
                        condition.SmartTriggerId = bo.Id;
                        item.SmartTriggerId = bo.Id;                        
                        var temp = conditionDomainService.Create(condition);
                        item.Id = temp.Id;
                        conditionIds.Add(temp.Id);
                    }
                }
                //删除
                var conditionItems = conditionDomainService.GetBySmartTriggerId(bo.Id);
                foreach (var q in conditionItems)
                {
                    if (!conditionIds.Contains(q.Id))
                    {
                        conditionDomainService.Delete(q.Id);
                    }
                }
            }
            if (bo.Actions != null && bo.Actions.Count > 0)
            {
                ISmartTriggerActionDomainService actionDomainService = _container.Resolve<ISmartTriggerActionDomainService>();
                List<Guid> actionIds = new List<Guid> { };

                foreach (var item in bo.Actions)
                {
                    if (item.Id != Guid.Empty)
                    {
                        //修改      
                        item.SmartTriggerId = bo.Id;
                        actionDomainService.Update(item);
                        actionIds.Add(item.Id);
                    }
                    else
                    {
                        //新增
                        SmartTriggerAction action = Mapper.Map<SmartTriggerAction>(item);
                        action.Id = Guid.NewGuid();
                        item.Id = action.Id;
                        action.SmartTriggerId = bo.Id;
                        item.SmartTriggerId = bo.Id;                        
                        var temp = actionDomainService.Create(action);
                        item.Id = temp.Id;
                        actionIds.Add(temp.Id);
                    }
                }
                //删除
                var actionItems = actionDomainService.GetBySmartTriggerId(bo.Id);
                foreach (var q in actionItems)
                {
                    if (!actionIds.Contains(q.Id))
                    {
                        actionDomainService.Delete(q.Id);
                    }
                }
            }

            _repository.Update(smartTrigger);

            return smartTrigger;
        }

        public IEnumerable<SmartTrigger> List()
        {
            return _repository.ListAll();
        }
        public SmartTrigger GetbyId(Guid id)
        {
            var result = _repository.Get(id);
            ISmartTriggerConditionDomainService conditionDomainService = _container.Resolve<ISmartTriggerConditionDomainService>();
            ISmartTriggerActionDomainService actionDomainService = _container.Resolve<ISmartTriggerActionDomainService>();
            result.Conditions = conditionDomainService.GetBySmartTriggerId(id).ToList();
            result.Actions = actionDomainService.GetBySmartTriggerId(id).ToList();
            return result;
        }
        public IEnumerable<SmartTrigger> GetbyBotId(Guid botId, string include = "")
        {
            var list = _repository.List(new SmartTriggerFilterSpecification(botId));
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include))
            {
                if (include.Equals("actions", StringComparison.OrdinalIgnoreCase))
                {
                    ISmartTriggerActionDomainService actionDomainService = _container.Resolve<ISmartTriggerActionDomainService>();
                    foreach (var item in list)
                    {
                        item.Actions = actionDomainService.GetBySmartTriggerId(item.Id).ToList();
                    }
                }
                if (include.Equals("conditions", StringComparison.OrdinalIgnoreCase))
                {
                    ISmartTriggerConditionDomainService conditionDomainService = _container.Resolve<ISmartTriggerConditionDomainService>();
                    foreach (var item in list)
                    {
                        item.Conditions = conditionDomainService.GetBySmartTriggerId(item.Id).ToList();
                    }
                }
            }
            return list;
        }
        public void Delete(Guid id)
        {
            SmartTrigger smartTrigger = _repository.Get(id);

            _repository.Delete(smartTrigger);

        }
    }
}
